** Building shocks in product space from changes in income distribution **
* Part 1 does this at the level of modules by quality deciles
* Part 2 does this at the level of UPCs

global db "D:\Dropbox\unequal_gains\main_data"

** PART 1: modules by quality deciles **

* 1. Get spending shares across modules by quality deciles

foreach i of numlist 2004(1)2006 {

* load data
use "$db/Important Price Datasets/price_index_household_income_`i'_2004_modules.dta", clear

* bring in info on UPC price decile (within product module) in base period:
merge m:1 upc upc_ver_uc using "$db/Important Lists/upc_brand_size_final.dta"
keep if _merge==3
drop _merge
sum size1_amount, d
drop if size1_amount<r(p1) | size1_amount>r(p99)
bysort upc upc_ver_uc: egen total_quantity_all=sum(total_quantity)
bysort upc upc_ver_uc: egen total_spending_all=sum(total_spending)
gen average_unit_price_all=total_spending_all/total_quantity_all
gen average_unit_price_adj=average_unit_price_all/(size1_amount*multi)
sum average_unit_price_adj, d
drop if average_unit_price_adj>r(p99) | average_unit_price_adj<r(p1)
* create quality ranks without weights
sort product_module_code average_unit_price_adj
bysort product_module_code: gen double temp=[_N]
bysort product_module_code: gen double temp2=[_n]
gen rank=temp2/temp*100
gen quality_rank=.
foreach r of numlist 1(1)10 {
replace quality_rank=`r' if rank<=`r'*10 & missing(quality_rank)
}

drop if missing(household_income)
gen income_quintile=.
* first quintile if makes below 20k
replace income_quintile=1 if household_income<13
* drop household who make below 5k
drop if household_income<4
* second quintile if makes between 20k & 40k
replace income_quintile=2 if household_income>12 & household_income<18
* third quintile if makes between 40k & 60k
replace income_quintile=3 if household_income>17 & household_income<23
* fourth quintile if makes between 60k & 100k 
replace income_quintile=4 if household_income>22 & household_income<27
* fifth quintile if makes above 100k
replace income_quintile=5 if household_income>26

* collapse product module total expenditures by group WITH projection weights
collapse (sum) total_spending [fw=projection_factor], ///
by(income_quintile product_module_code quality_rank) fast
save "$db/Important Datasets/`i'_spending_by_income_quintiles.dta", replace 
}

**** 2. Now compute spending shares across all three years by group [for robustness]

use "$db/Important Datasets/2004_spending_by_income_quintiles.dta", clear
append using "$db/Important Datasets/2005_spending_by_income_quintiles.dta"
append using "$db/Important Datasets/2006_spending_by_income_quintiles.dta"

cd "D:\Dropbox\unequal_gains\QJE revision plan\analysis\

collapse (sum) total_spending, by(product_module_code quality_rank income_quintile) fast
save income_quintiles_shares, replace

**** 3. Now build demand shocks in product space
** note: use only 2004 since other shares could be endogenous to shocks

use "$db/Important Datasets/2004_spending_by_income_quintiles.dta", clear
merge m:1 income_quintile using income_groups
keep if _merge==3
drop _merge
bysort product_module_code quality_rank: egen double spending_l=sum(total_spending)
gen double s_nl=total_spending/spending_l
gen instrument = (g_n_annual*100)*s_nl

collapse (sum) instrument total_spending, by(product_module_code quality_rank) fast

sum instrument [aw=total_spending], d
gen infl_hat_l=instrument*(-0.431)
gen full_infl_hat_l=instrument*(-0.618)

save instrument_income_quintiles, replace

** PART 2: UPC level **

* 1. Get spending shares

use "$db/Important Price Datasets/price_index_household_income_2004_2004_modules.dta", clear

drop if missing(household_income)
gen income_quintile=.
* first quintile if makes below 20k
replace income_quintile=1 if household_income<13
* drop household who make below 5k (outliers)
drop if household_income<4
* second quintile if makes between 20k & 40k
replace income_quintile=2 if household_income>12 & household_income<18
* third quintile if makes between 40k & 60k
replace income_quintile=3 if household_income>17 & household_income<23
* fourth quintile if makes between 60k & 100k 
replace income_quintile=4 if household_income>22 & household_income<27
* fifth quintile if makes above 100k
replace income_quintile=5 if household_income>26

* collapse product module total expenditures by group WITH projection weights
collapse (sum) total_spending [fw=projection_factor], ///
by(income_quintile upc upc_ver_uc) fast
save "$db/Important Datasets/2004_spending_by_income_quintiles_upc.dta", replace 

* 2. Now build demand shocks in product space
* note: use only 2004 since other shares could be endogenous to shocks
cd "D:\Dropbox\unequal_gains\QJE revision plan\analysis\

use "$db/Important Datasets/2004_spending_by_income_quintiles_upc.dta", clear
merge m:1 income_quintile using income_groups
keep if _merge==3
drop _merge
bysort upc upc_ver_uc: egen double spending_l=sum(total_spending)
gen double s_nl=total_spending/spending_l
gen instrument = (g_n_annual*100)*s_nl

collapse (sum) instrument total_spending, by(upc upc_ver_uc) fast

sum instrument [aw=total_spending], d
gen infl_hat_l=instrument*(-0.431)
gen full_infl_hat_l=instrument*(-0.618)

save instrument_income_quintiles_upc, replace


